<template>
  <!-- <el-dialog
    v-model="dialogVisible"
    title="飞书授权登录"
    width="30%"
    :before-close="handleClose"
  >
    <span>登录视为您已同意第三方账号绑定协议、服务条款、隐私政策系统</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleAuth">
          确定授权
        </el-button>
      </span>
    </template>
  </el-dialog> -->
  <div>
    飞书登录
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from '/@/store/modules/user'
import { Ref } from "vue";
defineOptions({
  name: 'FsLoginWay',
})

/**
 * @var
 */

const route = useRoute()
const userStore = useUserStore()
const socialLogin = (form: any) => userStore.socialLogin(form)
const redirect: Ref<any> = ref((route.query && route.query.redirect) || '/')
const proxy: any = getCurrentInstance()?.proxy
const dialogVisible = ref(false)

onMounted(() => {
  initFsLogin()
})

/**
 * @method
 */
// 飞书登录
const initFsLogin = () => {

  // 断言
  if (!(window as any).h5sdk) return;
  // 飞书
  (window as any).h5sdk.ready((res: any) => {
    const appId = import.meta.env.VITE_APP_BASE_FEISHU
    tt.requestAuthCode({
      appId,
      // 获取成功后的回调
      async success({ code }: any) {
        await socialLogin({ code, appId })
        setTimeout(() => { location.reload() }, 1000);
      },
      // 获取失败后的回调
      async fail(err: any) {
        debugger
        const { errno, errMsg } = err
        console.log(err, '错误信息');
        if (!errno) return
        proxy.$Message(errMsg, 'error')
      }
    })
  })
}

// 关闭
const handleClose = () => {
  dialogVisible.value = false
}
// 确定授权
const handleAuth = async () => {
  await initFsLogin()
}


/**
 * @实例
 */
watchEffect(() => {
  redirect.value = (route.query && route.query.redirect) || '/'
})


</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  height: 100vh;
  padding-top: 20vh;
  background: url('/@/assets/login_images/background.jpg') center center fixed no-repeat;
  background-size: cover;
}

@media (max-width: 576px) {
  .login-form {
    width: 85vw !important;
    margin: auto !important;

    .left-img {
      display: none !important;
    }

    .left-img2 {
      top: 35% !important;
      left: 24% !important;
    }

    .left-img1 {
      width: 100% !important;
    }

    .font2 {
      top: -2% !important;
      left: -2% !important;
    }

    .font1 {
      top: 20% !important;
      left: -2% !important;
    }

    .font3 {
      top: 99% !important;
      left: -2% !important;
    }

    :deep() {
      .el-form--default {
        width: 94% !important;
      }
    }
  }
}

.login-form {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 1000px;
  height: 550px;
  padding: 4.5vh;
  margin: auto;
  overflow: hidden;
  background: var(--el-color-white);
  background-size: 100% 100%;
  border-radius: 15px;

  .left-img {
    float: left;
    width: 50%;
  }

  .left-img1 {
    float: left;
    width: 50%;
    height: 100%;
    position: relative;
  }

  .left-img2 {
    position: absolute;
    top: 40%;
    left: 31%;
    margin: -50px 0 0 -50px;
  }

  :deep() {
    .el-form--default {
      float: left;
      width: 44%;
      margin-left: 5.8%;
    }

    .title {
      font-size: 54px;
      font-weight: 500;
      color: var(--el-color-black);
    }

    .title-tips {
      margin-top: 29px;
      font-size: 26px;
      font-weight: 400;
      color: var(--el-color-black);
    }

    .login-btn {
      display: inherit;
      width: 100%;
      height: 50px;
      margin-top: 15px;
      border: 0;

      &:hover {
        opacity: 0.9;
      }

      .forget-passwordword {
        width: 100%;
        margin-top: 40px;
        text-align: left;

        .forget-password {
          width: 129px;
          height: 19px;
          font-size: 20px;
          font-weight: 400;
          color: rgba(92, 102, 240, 1);
        }
      }
    }

    .tips {
      margin-bottom: 10px;
      font-size: var(--el-font-size-default);
      color: var(--el-color-white);

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .title-container {
      position: relative;

      .title {
        margin: 0 auto 40px auto;
        font-size: 34px;
        font-weight: bold;
        color: var(--el-color-primary);
        text-align: center;
      }
    }

    i {
      position: absolute;
      top: 9px;
      left: 15px;
      z-index: $base-z-index;
      font-size: 16px;
      color: var(--el-color-black);
      cursor: pointer;
      user-select: none;
    }

    .show-password {
      position: absolute;
      right: 25px;
      left: -35px;
      font-size: 16px;
      color: var(--el-color-black);
      cursor: pointer;
      user-select: none;
    }

    .el-form-item {
      padding-right: 0;
      margin: 20px 0;
      color: #454545;
      background: transparent;
      border: 1px solid transparent;
      border-radius: 2px;

      &__content {
        min-height: $base-input-height;
        line-height: $base-input-height;
      }

      &__error {
        position: absolute;
        top: 100%;
        left: 18px;
        font-size: var(--el-font-size-small);
        line-height: 18px;
        color: var(--el-color-error);
      }
    }

    .el-input {
      box-sizing: border-box;

      input {
        height: 48px;
        padding-left: 35px;
        font-size: var(--el-font-size-default);
        line-height: 48px;
        background: var(--el-color-white);
        border: 0;
      }
    }

    .code {
      position: absolute;
      top: 4px;
      right: 4px;
      cursor: pointer;
      border-radius: var(--el-border-radius-base);
    }
  }

  .font1 {
    position: absolute;
    top: 22%;
    left: 2%;
    width: 100%;
    height: 50px;
    display: block;
    overflow-wrap: break-word;
    color: #000;
    font-size: 24px;
    letter-spacing: .40000001px;
    font-family: PingFangSC-Medium;
    font-weight: 500;
    text-align: center;
    white-space: nowrap;
  }

  .font2 {
    position: absolute;
    top: 0%;
    left: 2%;
    display: block;
    width: 100%;
    height: 67px;
    overflow-wrap: break-word;
    color: #636366;
    font-size: 36px;
    font-family: PingFangSC-Medium;
    font-weight: 500;
    white-space: nowrap;
    line-height: 67px;
    text-align: center;
  }

  .font3 {
    position: absolute;
    top: 103%;
    left: 6%;
    display: block;
    width: 100%;
    height: 22px;
    color: #636366;
    font-size: 5px;
    font-family: PingFangSC-Medium;
    font-weight: 500;
    text-align: center;
  }
}
</style>
